package com.eh.taxation.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eh.commonutils.DateUtil;
import com.eh.commonutils.R;
import com.eh.taxation.entity.Question;
import com.eh.taxation.entity.vo.QuestionWebInfo;
import com.eh.taxation.service.QuestionService;
import com.eh.taxation.service.SubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/taxation/indexfront")
public class IndexFrontController {

    @Autowired
    private QuestionService questionService;

    @Autowired
    private SubjectService subjectService;

    // 获取热门问答
    @GetMapping("getHotQuestion")
    public R getHotQuestion() {
        List<Question> questionList = questionService.getHotQuestion();
        return R.ok().data("questionList", questionList);
    }

    // 搜索问答
    @GetMapping("getSearchResult/{page}/{limit}/")
    public R getSearchResultWithoutKeyword(@PathVariable long page,
                             @PathVariable long limit) {
        String keyword = "";
        Page<Question> questionPage = new Page<>(page, limit);
        Map<String, Object> map = questionService.getPageListBykeyword(questionPage, keyword);
        return R.ok().data(map);
    }

    // 搜索问答
    @GetMapping("getSearchResult/{page}/{limit}/{keyword}")
    public R getSearchResult(@PathVariable long page,
                            @PathVariable long limit,
                            @PathVariable String keyword) {
        Page<Question> questionPage = new Page<>(page, limit);
        Map<String, Object> map = questionService.getPageListBykeyword(questionPage, keyword);
        return R.ok().data(map);
    }

    // 根据问题ID查询问题
    @GetMapping("getQuestionInfo/{questionId}")
    public R getQuestionInfo(@PathVariable String questionId) {
        QuestionWebInfo questionWebInfo = new QuestionWebInfo();
        Question question = questionService.getById(questionId);
        questionWebInfo.setId(question.getId());
        questionWebInfo.setContent(question.getContent());
        questionWebInfo.setTitle(question.getTitle());
        questionWebInfo.setSubjectParentName(subjectService.getById(question.getSubjectParentId()).getTitle());
        questionWebInfo.setSubjectName(subjectService.getById(question.getSubjectId()).getTitle());
        questionWebInfo.setUpdateTime(DateUtil.formatDate(question.getGmtModified()));
        return R.ok().data("questionInfo", questionWebInfo);
    }

}
